/*
========================================================================
 Name        : Test9ContainerView.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>
#include <aknnotewrappers.h>
#include <aknquerydialog.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <Test9.rsg>
// ]]] end generated region [Generated System Includes]
#include "Settings.h"
// [[[ begin generated region: do not modify [Generated User Includes]

#include "Test9.hrh"
#include "Test9ContainerView.h"
#include "Test9Container.hrh"
#include "Test9ListBox.hrh"
#include "Test9Container2.hrh"
#include "Test9Container.h"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CTest9ContainerView::CTest9ContainerView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iTest9Container = NULL;
	// ]]] end generated region [Generated Contents]
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CTest9ContainerView::~CTest9ContainerView()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	delete iTest9Container;
	iTest9Container = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CTest9ContainerView
 */
CTest9ContainerView* CTest9ContainerView::NewL()
	{
	CTest9ContainerView* self = CTest9ContainerView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CTest9ContainerView
 */
CTest9ContainerView* CTest9ContainerView::NewLC()
	{
	CTest9ContainerView* self = new ( ELeave ) CTest9ContainerView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CTest9ContainerView::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Code]
	BaseConstructL( R_TEST9_CONTAINER_TEST9_CONTAINER_VIEW );
				
	// ]]] end generated region [Generated Code]
	
	// add your own initialization code here
	
	}

/**
 * @return The UID for this view
 */
TUid CTest9ContainerView::Id() const
	{
	return TUid::Uid( ETest9ContainerViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CTest9ContainerView::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{	// code to dispatch to the AknView's menu and CBA commands is generated here
		case ETest9ContainerViewStorageMenuItemCommand:
			commandHandled = HandleStorageMenuItemSelectedL( aCommand );
			break;
		case ETest9ContainerViewStepsMenuItemCommand:
			commandHandled = HandleStepsMenuItemSelectedL( aCommand );
			break;
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
	
		}
	// ]]] end generated region [Generated Code]
	
	if ( !commandHandled ) 
		{
		if ( aCommand == EAknSoftkeyExit )
			{
			AppUi()->HandleCommandL( EAknSoftkeyExit );
			}
		}
	
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CTest9ContainerView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	SetupStatusPaneL();
	
				
	if ( iTest9Container == NULL )
		{
		iTest9Container = CreateContainerL();
		iTest9Container->SetMopParent( this );
		AppUi()->AddToStackL( *this, iTest9Container );
		} 
	// ]]] end generated region [Generated Contents]
	
	}

/**
 */
void CTest9ContainerView::DoDeactivate()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CleanupStatusPane();
	
	if ( iTest9Container != NULL )
		{
		AppUi()->RemoveFromViewStack( *this, iTest9Container );
		delete iTest9Container;
		iTest9Container = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CTest9ContainerView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}

// [[[ begin generated function: do not modify
void CTest9ContainerView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_TEST9_CONTAINER_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

// ]]] end generated function

// [[[ begin generated function: do not modify
void CTest9ContainerView::CleanupStatusPane()
	{
	}

// ]]] end generated function

/**
 *	Creates the top-level container for the view.  You may modify this method's
 *	contents and the CTest9Container::NewL() signature as needed to initialize the
 *	container, but the signature for this method is fixed.
 *	@return new initialized instance of CTest9Container
 */
CTest9Container* CTest9ContainerView::CreateContainerL()
	{
	return CTest9Container::NewL( ClientRect(), NULL, this );
	}

// [[[ begin generated function: do not modify
/**
 * Show the popup note for note1
 * @param aOverrideText optional override text
 */
void CTest9ContainerView::RunNote1L( const TDesC* aOverrideText )
	{
	CAknConfirmationNote* note = new ( ELeave ) CAknConfirmationNote();
	if ( aOverrideText == NULL )
		{
		HBufC* noteText = StringLoader::LoadLC( R_TEST9_CONTAINER_NOTE1 );
		note->ExecuteLD( *noteText );
		CleanupStack::PopAndDestroy( noteText );
		}
	else
		{
		note->ExecuteLD( *aOverrideText );
		}
	}
// ]]] end generated function

// [[[ begin generated function: do not modify
/**
 * Show the popup dialog for confQuery1
 * @param aOverrideText optional override text
 * @return EAknSoftkeyYes (left soft key id) or 0
 */
TInt CTest9ContainerView::RunConfQuery1L( const TDesC* aOverrideText )
	{
				
	CAknQueryDialog* queryDialog = CAknQueryDialog::NewL();	
	
	if ( aOverrideText != NULL )
		{
		queryDialog->SetPromptL( *aOverrideText );
		}
	return queryDialog->ExecuteLD( R_TEST9_CONTAINER_CONF_QUERY1 );
	}
// ]]] end generated function

// [[[ begin generated function: do not modify
/**
 * Show the popup dialog for dataQuery1
 * @param aData in-out TDes data
 * @param aUseDefaults TBool use designer default editor data if ETrue
 * @param aOverridePrompt optional override prompt text
 * @return EAknSoftkeyOk (left soft key id) or 0
 */
TInt CTest9ContainerView::RunDataQuery1L( 
		TDes& aData, 
		TBool aUseDefaults, 
		const TDesC* aOverridePrompt )
	{
	if ( aUseDefaults )
		{
		HBufC* text = StringLoader::LoadLC( R_TEST9_CONTAINER_EDIT1 );
		aData.Copy( *text );
		CleanupStack::PopAndDestroy( text );
		}
				
	CAknTextQueryDialog* queryDialog = CAknTextQueryDialog::NewL( aData );	
	
	if ( aOverridePrompt != NULL )
		{
		CleanupStack::PushL( queryDialog );
		queryDialog->SetPromptL( *aOverridePrompt );
		CleanupStack::Pop(); // queryDialog
		}
	
	return queryDialog->ExecuteLD( R_TEST9_CONTAINER_DATA_QUERY1 );
	}
// ]]] end generated function

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CTest9ContainerView::HandleStorageMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	_LIT(phoneMemory, "Phone Memory");
	_LIT(memoryCard, "Memory Card");
	TBuf<128> question;
	question.Append(_L("You are using "));
	if(CSettings::theInstance()->isPhoneMemory)
		question.Append(phoneMemory);
	else
		question.Append(memoryCard);
	question.Append(_L(" to store trip files do you want to use "));
	if(CSettings::theInstance()->isPhoneMemory)
		question.Append(memoryCard);
	else
		question.Append(phoneMemory);
	question.Append(_L(" instead?"));
	
	
	if(RunConfQuery1L(&question) != 0)
		{
		CSettings::theInstance()->isPhoneMemory = !CSettings::theInstance()->isPhoneMemory;
		}
		
	return ETrue;
	}

/** 
 * Handle the selected event.
 * @param aCommand the command id invoked
 * @return ETrue if the command was handled, EFalse if not
 */
TBool CTest9ContainerView::HandleStepsMenuItemSelectedL( TInt aCommand )
	{
	// TODO: implement selected event handler
	_LIT(question, "Enter the number of steps per click");
	TBuf<50> aData;
	aData.AppendNum(CSettings::theInstance()->stepsPerClick);
	if(RunDataQuery2L(aData, true, &question) != 0)
		{
		TLex lex(aData);
		User::LeaveIfError(lex.Val(CSettings::theInstance()->stepsPerClick));
		}
	return ETrue;
	}
				
// [[[ begin generated function: do not modify
/**
 * Show the popup dialog for dataQuery2
 * @param aData in-out TDes data
 * @param aUseDefaults TBool use designer default editor data if ETrue
 * @param aOverridePrompt optional override prompt text
 * @return EAknSoftkeyOk (left soft key id) or 0
 */
TInt CTest9ContainerView::RunDataQuery2L( 
		TDes& aData, 
		TBool aUseDefaults, 
		const TDesC* aOverridePrompt )
	{
	if ( aUseDefaults )
		{
		//HBufC* text = StringLoader::LoadLC( R_TEST9_CONTAINER_EDIT2 );
		//aData.Copy( *text );
		//CleanupStack::PopAndDestroy( text );
		}
				
	CAknTextQueryDialog* queryDialog = CAknTextQueryDialog::NewL( aData );	
	
	if ( aOverridePrompt != NULL )
		{
		CleanupStack::PushL( queryDialog );
		queryDialog->SetPromptL( *aOverridePrompt );
		CleanupStack::Pop(); // queryDialog
		}
	
	return queryDialog->ExecuteLD( R_TEST9_CONTAINER_DATA_QUERY2 );
	}
// ]]] end generated function

